<html>
<h1>
SQL Query Interface
</h1>
<body>
(Ver 1.0 4/22/12 by David Meng and Matthew Kwong) </br>
Type a valid SQL "SELECT" statement in the following box (e.g., SELECT * FROM Actor WHERE id < 20). </br>
<form action="query.php" method="post">
<textarea name="input" rows=10 cols=50></textarea>
<input type="submit" value="GO!" />
</form>
<?php
if($_POST["input"]) {

$query = $_POST["input"];

$db_connection = mysql_connect("localhost", "cs143", "");
mysql_select_db("CS143", $db_connection);

$rs = mysql_query($query, $db_connection);

echo "<table border=1>";

$num = mysql_num_fields($rs);

echo "<tr>";
$i=0;
while($i < $num)
{
	$col = mysql_fetch_field($rs,$i);
	echo "<td><b>$col->name</b></td>";
	$i++;
}
echo "</tr>";

while($row = mysql_fetch_row($rs)) 
{
	echo "<tr>";
	$j=0;
	while($j < $num)
	{
		$col = mysql_fetch_field($rs,$j);
		$field = $row[$j];
		if($col->name == "dod" && $field == NULL)
		{  echo "<td>N/A</td>";  }
		else
		{  echo "<td>$field</td>";  }
		$j++;
	}
	echo "</tr>";
}	
echo "</table>";

mysql_close($db_connection);

}
?>
</body>
</html>
